//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcTextView.h"

@implementation smcTextView

#pragma mark Usual
- (id) initWithFrame: (CGRect) rect
{
	self = [super initWithFrame: rect];
	if (self != nil)
	{
		// self is tapped
		[self setTapDelegate: self];
		_allowReturn = NO;
		_smsLength = YES;
	}
	return self;
}

#pragma mark overridden, not delegated

- (void)webViewDidChange: (NSNotification *) notification
{
	// send delegate note (for showing chars left)
	if ([[self delegate] respondsToSelector: @selector(textViewDidChange:)])
		[[self delegate] performSelector: @selector(textViewDidChange:) withObject: self];

	[super webViewDidChange: notification];
}

- (BOOL)webView:(id)fp8 shouldInsertText: (id) insert replacingDOMRange:(id)fp16 givenAction:(int)fp20
{
	// activate next responder if !_allowReturn and return key
	if ([insert isEqualToString: @"\n"] && !_allowReturn)
	{
		if ([[self delegate] respondsToSelector: @selector(textViewReturnWithoutAllowance:)])
			[[self delegate] performSelector: @selector(textViewReturnWithoutAllowance:) withObject: self];
		return NO;
	}
	
	if (([[self text] length] + [insert length] > 140) && _smsLength)
		return NO;
	else
		return [super webView: fp8 shouldInsertText: insert replacingDOMRange: fp16 givenAction: fp20];
}

#pragma mark Set and Get

- (BOOL) returnAllowed
{
	return _allowReturn;
}

- (void) setReturnAllowed: (BOOL) yn
{
	_allowReturn = yn;
}

- (BOOL) onlySMSLength
{
	return _smsLength;
}

- (void) setOnlySMSLength: (BOOL) yn
{
	_smsLength = yn;
}

#pragma mark Delegates

- (BOOL)view:(id)view handleTapWithCount: (int) count event: (struct __GSEvent *) event
{
	// show keyboard if single tap in view
	// hide when double tapped

	if (count == 2)
	{
		if ([[self delegate] respondsToSelector: @selector(shouldHideKeyboardForView:)])
			[[self delegate] performSelector: @selector(shouldHideKeyboardForView:) withObject: self];
	} 
	else if ([[self delegate] respondsToSelector: @selector(shouldShowKeyboardForView:)])
			[[self delegate] performSelector: @selector(shouldShowKeyboardForView:) withObject: self];
			
	return YES;
}

@end
